<?php
/*
 * Created on Sep 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
require_once APPPATH."models/model_dao.php";
class Model_news extends Model_dao {
	var $_ispublish = 0;

	function Model_news() {
		// Call the Model constructor
		parent :: Model_dao('tbl_news');
	}

	function find($id) {
		$this->db->where("id", $id);
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->first_row('array');
		}
		return null;
	}

	function find_by_group($gid, $order_by = 'sort', $method = "ASC")
	{
		$this->db->where("gid", $gid);
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function browse($keyword='', $group='', $fromdate='', $todate='', $limit = 10, $offset = 0, $order_by = 'sort', $method = "ASC"){
		$this->db->select("{$this->_tbl_name}.*, tbl_news_gr.name as group_name");
		$this->db->from($this->_tbl_name);
		if(!empty($keyword)){
			$this->db->like($this->_tbl_name.'.name', $keyword);
			$this->db->or_like($this->_tbl_name.'.description', $keyword);
		}
		if(!empty($group)){
			$this->db->where('gid', $group);
		}
		if(!empty($fromdate) AND !empty($todate)){
			$this->db->where(array('postdate <' => $todate, 'postdate >' => $fromdate));
		}
		elseif(!empty($fromdate)){
			$this->db->where('postdate >' , $fromdate);
		}
		elseif(!empty($todate)){
			$this->db->where('postdate <' , $todate);
		}
		$this->db->where('publish' , $this->_ispublish);
		// join with group
		$this->db->join('tbl_news_gr', "tbl_news_gr.id = {$this->_tbl_name}.gid", "left");

		$this->db->order_by($order_by, $method);
		$this->db->limit($limit, $offset);

		$query = $this->db->get();
		//echo $this->db->last_query();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function browse_count($keyword, $group='', $fromdate='', $todate=''){
		if(!empty($keyword)){
			$this->db->like('name', $keyword);
			$this->db->or_like('description', $keyword);
		}
		if(!empty($group)){
			$this->db->where('gid', $group);
		}
		if(!empty($fromdate) AND !empty($todate)){
			$this->db->where(array('postdate <' => $todate, 'postdate >' => $fromdate));
		}
		elseif(!empty($fromdate)){
			$this->db->where('postdate >' , $fromdate);
		}
		elseif(!empty($todate)){
			$this->db->where('postdate <' , $todate);
		}
		$this->db->where('publish' , $this->_ispublish);
		return $this->db->count_all_results($this->_tbl_name);
	}

	/**
	 *
	 *
	 */
	function find_by_ids($ids, $order_by="tbl_news.sort", $method="ASC"){
		$this->db->select("{$this->_tbl_name}.*, tbl_news_gr.nam as group_name");
		$this->db->join('tbl_news_gr', "tbl_news_gr.id = {$this->_tbl_name}.gid");
		$this->db->order_by($order_by, $method);

		$this->db->where_in("{$this->_tbl_name}.id", $ids);
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function insert_news($data)
	{
		$data['createdate'] = date("Y-m-d");
		$result = $this->db->insert($this->_tbl_name, $data);
		//echo $this->db->last_query();
		return $result;
	}

	/**
	 *
	 *
	 */
	function update_news($data, $id)
	{
		if(intval($id)){
			$this->db->where('id', $id);
			$result = $this->db->update($this->_tbl_name, $data);
			return $result;
		}
	}

	/**
	 *
	 *
	 */
	function delete_news($id){
		// not detete item in group
		$this->db->where('id', $id);
		return $this->db->delete($this->_tbl_name);
	}

	/**
	 *
	 *
	 */
	function get_max_sort()
	{
		$this->db->select_max('sort');
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['sort'];
		}
		return 0;
	}

	/**
	 *
	 *
	 */
	function get_max_date(){
		$this->db->select_max('postdate');
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['postdate'];
		}
		return date('Y-m-d');
	}

	function updatePostdate(){
		$sql = "SELECT * FROM {$this->_tbl_name}";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $item) {
				$oldstr = $item['postdate'];
				$newstr = '20'.substr($oldstr, 6, 2) . '-' . substr($oldstr, 3, 2) . '-' . substr($oldstr,0,2);
				//echo $newstr;
				$data = array('postdate' => $newstr);

				if(!empty($item['img'])){
					$data['img'] = substr($item['img'], 9);
				}
				//self::update_news($data, $item['id']);
			}
		}
	}
}
?>